# -*- coding: utf-8 -*-
# @Time    : 2019/11/26 0026 9:18
# @Author  : wulin
# @Email   :286075568@qq.com
# @FileName: runhtml.py
# @Software: PyCharm


import unittest, sys, time, os
from BeautifulReport import BeautifulReport as bf
from Unittest import datadriven

from common.outlog import MyLog
from common.mysql import Mysql
from common.B2Bmysql import B2BMysql
from common.deletelog import compare_time

# 运行的相对路径
path = '.'
# 用例路径
casepath = ''
resultpath = ''
logger = MyLog('INFO')

if __name__ == '__main__':

    # casename='Web'
    # casename='http_ydh'
    casename = 'B2Binter'

    if casename.startswith('B2B'):
        # 用于删除B2B数据
        mysql = B2BMysql()
        mysql.init_mysql()

    else:
        # 用于删除新零售数据
        mysql = Mysql()
        mysql.init_mysql()



    # 执行
    try:
        casepath = sys.argv[1]
    except:
        casepath = ''

    # 为空，则使用默认的
    if casepath == '':
        casepath = path + '/excel_case/'+casename+'.xlsx'
        # resultpath = ''
        resultpath = path + '/test_result/excel_result/result_'+casename+'.xlsx'
    else:
        # 如果是绝对路径，就使用绝对路径
        if casepath.find(':') >= 0:
            # 获取用例文件名
            resultpath = path + '/test_result/excel_result/result-' + casepath[casepath.rfind('\\') + 1:]

        else:
            logger.error('用例路径不存在!')

    datadriven.getparams(casepath, resultpath)

    suite = unittest.defaultTestLoader.discover("./Unittest/", pattern="HttpTest.py", top_level_dir=None)
    # 生成执行用例的对象
    runner = bf(suite)
    runner.report(filename='./test_result/html_report/'+casename+'.html', description=datadriven.title)
    datadriven.writer.save_close()
    # 用于删除日志文件
    now = time.strftime('%Y-%m-%d')
    filePath = './test_result/log_txt'
    dir = os.listdir(filePath)

    for path in dir:
        data = path.split('_')
        data = data[2]
        data1 = data.split('.')
        data2 = data1[0]
        compare_time(data2, now)
